#include <avr/io.h>
#include <avr/interrupt.h>
#include <stdlib.h>
#include <avr/interrupt.h>
#include <inttypes.h>
#include <util/delay.h>
#include "sonar.h"



int direction = 0;

void init_pwm_motor () {



	//Begin initializing PWM on Timer0
	DDRC = 0x80; //Set Timer0A PWM (0x80) output as an output
	DDRD = 0x01; //Set Timer0B PWM (0x01) output as an output

	// Set the Timer0 to PWM Fast Mode
	TCCR0A = (_BV(WGM00)) | (_BV(WGM01)) | (_BV(COM0A1)) | (_BV(COM0B1));
	//Set the Timer0 for non-inverting mode
	//Set the Timer on with no prescaler
	TCCR0B = (_BV(CS00));

	//Set the value of the duty cycle as a percentage.
	OCR0A = 0xff;
	OCR0B = 0xff;


	//DDRC | = _BV(5) //pin 5 on port C
	//PORTC ^= _BV(PORTC5);
	
	PORTD = 0xf0; //turn on red led
	PORTC = 0x20;
	for (;;){
	 if (direction == 0) {
	  
	  _delay_ms(5000000);
	  PORTC |= (1<<PORTC5); //turn on motor direction 0
	  PORTC ^= _BV(PORTC5); //clear pin 5
	 } else {
	    
	  	_delay_ms(5000000); 
	  	PORTC |= (1>>PORT5); //direction `
		PORTC ^= _BV(PORTC5); //clear pin 5
	
	 }
	 changeDirection();
	}
	//change direction



}

void changeDirection() {
	if(direction ==0){
		direction =1;
	} else {
		direction = 1;
	}

}

int main (int argc, char *argv[]) {

	init_pwm_motor();
	initTimer();
	return 0;

}
